const moduleMap = require('../model/moduleMap')

function getNotificationEntryName(entry) {
  const keys = getNotificationKeys(entry)
  const group = getNotificationGroup(keys)
  const cnGroup = transformEntryGroupToChinese(group)
  const content = formatGroup(cnGroup)
  
  return content
}

function getNotificationKeys(entry) {
  return Object.keys(entry)
}

function getNotificationGroup(entryKeys) {
  let group = {}
  
  entryKeys.forEach(key => {
    
    let startKey = key.split('.')[0]
    
    if (!group[startKey]) {
      group[startKey] = []
    }
    
    group[startKey].push(key)
    
  })
  
  return group
}

function transformEntryGroupToChinese(group = {}) {
  const transformMap = {}
  
  Object.keys(group).forEach(key => {
    
    let cnKey = `${key}  ${moduleMap[key]}`
    
    transformMap[cnKey] = group[key].map(item => {
      return `            ${item}  ${moduleMap[item]}`
    })
    
  })
  
  return transformMap
}

function formatGroup(group) {
  
  let text = ''
  
  Object.keys(group).forEach(key => {
    
    text += `${key}:\n`
    
    group[key].forEach(item => {
      text += `  ${item}\n`
    })
    
    text += '\n'
    
  })
  
  return text
  
}

module.exports = getNotificationEntryName;